<?php namespace App\Http\Controllers;

use App\Admin;
use App\Banner;
use App\BooksChapter;
use App\BooksInfo;
use App\Classify;
use App\Http\Controllers\Api\AudioController;
use App\Http\Services\FileManager;
use App\UserVip;
use App\VipRule;
use Blocktrail\CryptoJSAES\CryptoJSAES;
use App\RechargePackage;
use App\User;
use App\UserInfo;
use App\UserSpendRecord;
use Illuminate\Contracts\Auth\Guard;
use DB;
use Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;

class AdminController extends Controller
{
    public function login(){
        return view('admin.login');
    }

    public function bannerList(){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        /** @var Banner $sysBanner */
        $sysBanners = Banner::orderBy('sort','asc')->orderBy('created_at','desc')->get();
        if(count($sysBanners) > 0){
            foreach($sysBanners as $sysBanner){
                $sysBanner->img_url = FileManager::getImgUrl($sysBanner->img_id);
            }
        }

        return view('admin.banner-list',['banners'=>$sysBanners]);
    }

    public function bannerSet(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        /** @var Banner $thatBanner */
        $thatBanner = Banner::whereId($id)->first();
        if($thatBanner){
            $thatBanner->img_url = FileManager::getImgUrl($thatBanner->img_id);
        }

        return view('admin.banner-set',['banner'=>$thatBanner]);
    }

    public function userList(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $users = User::orderBy("created_at","desc")->paginate(50);

        foreach($users as $user){
            /** @var User $user */
            $user->userInfo;
            $user->userInfo->photo_url = FileManager::getImgUrl($user->userInfo->photo_id);
        }

        return view('admin.user-list',["users"=>$users]);
    }

    public function adminList(Request $request)
    {
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        /** @var Admin $newAdmin */
        $newAdmins = Admin::orderBy('created_at', 'desc')->paginate(10);

        return view('admin.admin-list', ['admins' => $newAdmins]);
    }

    public function adminSet(Request $request)
    {
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        return view('admin.admin-set');
    }

    public function changePwd(){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        return view('admin.change-psw');
    }

    public function classifyList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $classify = Classify::orderBy('sort','asc')->orderBy('created_at','desc')->get();

        return view('admin.classify-list',['classify' => $classify]);
    }
}